<?php 

class MultipleSelectFieldType extends GigaFieldType
{
	function onModelInitialize($model)
	{
		$fields = $model->getFields('type', 'multipleSelect');
		foreach($fields as $field)
		{
			$model->additionalFields[] = $field;
			$model->additionalFieldMethod[$field] = array($this, 'fetchValue');
		}
		//prd($model->fieldProperties);
	}

	
	/**
	 * Additional field function to fetch values 
	 * @return unknown_type
	 */
	function fetchValue($model, $field, $result)
	{
		$pk = $result[$model->alias]['id'];
		$tmp = $model->getFieldProperty($field, 'storageModel');
		$records = array();
		if($tmp)
		{
			$storageModel = cri($tmp[0]);
			$field = $tmp[1];
		
			$fields = array($storageModel->alias.'.'.$field);
			$conditions = array($storageModel->alias.'.'.$model->foreignKey => $pk);
			$records = $storageModel->find('all', compact('conditions', 'fields'));
			$records = Set::extract('{n}.'.$storageModel->alias.'.'.$field, $records);
		}else
		{
			$records = array();
			if(isset($result[$model->alias][$field]))
			{
				$records = $result[$model->alias][$field];
			}
			//$records = $result
		}
		return $records;
	}
	
	
	function getFormOptions($form, $field, $options)
	{
		if(!empty($form->data))
		{
			prd($form->data);
		}
	}
	
	function beforeSave1($model)
	{
		//pr($model->data);
	}
	
	function afterSave($model, $created)
	{
		$fields = $model->getFields('type', 'multipleSelect');
		foreach($fields as $field)
		{
			$tmp = $model->getFieldProperty($field, 'storageModel');
			if($tmp)
			{
					$child_model = $tmp[0];
					$child_field = $tmp[1];
					$child_model = cri($child_model);
					if(!is_array($model->data[$model->alias][$field]))
					{
						continue;
					}
					$childRecords = array();
					$deleteConditions = array($child_model->alias.'.'.$model->foreignKey => $model->id);
					//pr($deleteConditions);
					$child_model->deleteAll($deleteConditions);
					foreach($model->data[$model->alias][$field] as $option)
					{
						$child_model->id = '';
						$newChildRecord = array(
						$child_model->alias => array(
						$child_field => $option,
						$model->foreignKey => $model->id
						)
						);
						$child_model->save($newChildRecord);
					}
			}
			
		}
	}
	
	function beforeSaveAll1($model)
	{
		//echo "called";
		//prd($model->saveAllData);
		$fields = $model->getFields('type', 'multipleSelect');
		foreach($model->saveAllData[$model->alias] as $key => $record)
		{
			foreach($fields as $field)
			{
				if(isset($model->saveAllData[$model->alias][$key][$field]))
				{
					$tmp = $model->getFieldProperty($field, 'storageModel');
					$child_model = $tmp[0];
					$child_field = $tmp[1];
					$child_model = cri($child_model);
	
					if(isset($model->saveAllData[$model->alias][$key][$field]))
					{
						$selectedOptions = $model->saveAllData[$model->alias][$key][$field];
						if(!is_array($selectedOptions))
						{
							continue;
						}
						$i = 0;
						foreach($selectedOptions as $option)
						{
							$model->saveAllData[$child_model->alias][$key][$i][$child_field] = $option;
							$i++;
						}
					}
					
				}
				unset($model->saveAllData[$model->alias][$key][$field]);
			}
		}
	}	
}
?>